﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplicationLibrary.SqlConnector;
using Plumber.Sales.ModuleData;
using ApplicationLibrary;
using Plumber.Masters.ModuleData;

namespace Plumber.Sales.Library
{
    public class SalesInvoiceUpdate10
    {
        public bool UpdateInvoice()
        {
            var dRTaxCode = new DRTaxCode(false);
            var qb = new QueryBuilder(false, true);
            var dRSalesInvoices = new DRSalesInvoices(true);

            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);
            qb.AddCompare<string>(PL_SalesInvoices.SalePayed, "0");
            qb.Where += string.Format(" AND ({0} = '1' OR {1} = '1' OR {2} = '1')",
                PL_SalesInvoices.Restraint.Name,
                PL_SalesInvoices.Deduction.Name,
                PL_SalesInvoices.BankRestraint.Name);

            SqlABDataReader dr = qb.ExecuteReader();
            while(dr.Read())
            {
                Double taxPerc = 0;
                Double Total = 0;
                Double Amount = dr.GetValue<double>(PL_SalesInvoices.Amount);
                Double Tax = 0;
                Double TaxRest = 0;
                Double Restraint = 0;
                Double Deduction = 0;
                Double BankRestraint = 0;

                if (dRTaxCode.Find(dr.GetValue<string>(PL_SalesInvoices.TaxCode)))
                    taxPerc = 1 + dRTaxCode.GetValue<double>(PL_TaxCode.Percent) / 100;

                Total = taxPerc == 0
                            ? Amount
                            : Amount * taxPerc;

                Tax = Total - Amount;

                if (dr.GetValue<bool>(PL_SalesInvoices.Restraint) && dRTaxCode.Find(dr.GetValue<string>(PL_SalesInvoices.RestraintCode)))
                {
                    TaxRest = dRTaxCode.GetValue<double>(PL_TaxCode.Percent) / 100;
                    Restraint = dr.GetValue<bool>(PL_SalesInvoices.Restraint)
                                            ? Amount * TaxRest
                                            : 0;
                    Total -= Restraint;
                }

                if (dr.GetValue<bool>(PL_SalesInvoices.Deduction))
                {
                    Deduction = Math.Round(Total / 1.21 * 0.04, 2);
                    Total -= Deduction;
                }

                if (dr.GetValue<bool>(PL_SalesInvoices.BankRestraint))
                {
                    BankRestraint = Math.Round(Total / 1.22 * 0.08, 2);
                    Total -= BankRestraint;
                }

                if (!dRSalesInvoices.Find(dr.GetValue<InvoiceType>(PL_SalesInvoices.InvoiceType), dr.GetValue<string>(PL_SalesInvoices.InvoiceNo)))
                    return false;

                dRSalesInvoices.SetValue<double>(PL_SalesInvoices.Tax, Tax);
                dRSalesInvoices.SetValue<double>(PL_SalesInvoices.RestraintAmount, Restraint);
                dRSalesInvoices.SetValue<double>(PL_SalesInvoices.DeductionAmount, Deduction);
                dRSalesInvoices.SetValue<double>(PL_SalesInvoices.BankRestraintAmount, BankRestraint);
                dRSalesInvoices.SetValue<double>(PL_SalesInvoices.Total, Total);

                if (dr.GetValue<double>(PL_SalesInvoices.AmountPayed) == Total)
                    dRSalesInvoices.SetValue<bool>(PL_SalesInvoices.SalePayed, true);

                dRSalesInvoices.Update();
            }
            return true;
        }
    }
}
